package com.ldc.commonlibs.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.ldc.commonlibs.views.StateViewLayout;

/**
 * BaseActivity
 *
 * @author liudc
 * @since 2022/7/3 13:25
 */
public abstract class ActivityBase extends AppCompatActivity {
    protected Activity mActivity;
    private StateViewLayout stateViewLayout;
    protected final String TAG = this.getClass().getCanonicalName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            setContentView(getLayoutView());
            mActivity = this;
            stateViewLayout = initStateViewLayout();
            preMain(savedInstanceState);
            main(savedInstanceState);
        } catch (Exception e) {
            processError(e);
        }
    }

    /**
     * 加载视图
     *
     * @return View
     */
    protected View getLayoutView() {
        return null;
    }

    /**
     * 初始化主函数前
     *
     * @param savedInstanceState 数据
     */
    protected void preMain(@Nullable Bundle savedInstanceState) {
    }

    /**
     * 主函数
     *
     * @param savedInstanceState 数据
     */
    protected abstract void main(@Nullable Bundle savedInstanceState);

    /**
     * 处理错误
     *
     * @param error 异常
     */
    protected void processError(Exception error) {
        error.printStackTrace();
    }

    /**
     * 初始化转态
     *
     * @return 状态视图
     */
    protected StateViewLayout initStateViewLayout() {
        return null;
    }

    /**
     * @return 转态视图
     */
    public StateViewLayout getStateViewLayout() {
        return stateViewLayout;
    }

    // 显示内容
    public void showContentView() {
        if (stateViewLayout != null) {
            stateViewLayout.showContentView();
        }
    }

    // 显示成功页面
    public void showSuccessView() {
        if (stateViewLayout != null) {
            stateViewLayout.showSuccessView();
        }
    }

    public void showEmptyView() {
        if (stateViewLayout != null) {
            stateViewLayout.showEmptyView();
        }
    }

    // 显示加载
    public void showLoadingView() {
        if (stateViewLayout != null) {
            stateViewLayout.showLoadingView();
        }
    }

    // 显示错误
    public void showErrorView() {
        if (stateViewLayout != null) {
            stateViewLayout.showErrorView();
        }
    }
}
